package com.jxufe.getData;

import java.util.Arrays;
import java.util.List;

import org.apache.log4j.Logger;

import com.jxufe.weather.WeaBean;
import com.jxufe.weather.WeaStatBean;

public class Test {
	private static Logger logger = Logger.getLogger(Test.class);

	public static void main(String[] args) throws Exception {
		System.out.println(Resouce.cityAmount);

		logger.debug("This is debug message.");
		// 记录info级别的信息
		logger.info("This is info message.");
		// 记录error级别的信息
		logger.error("This is error message.");
	}

//	@org.junit.Test
	public WeaBean mapTest(String str) {
//		String str = "101150603 治多 7 2019-08-09 11:30:00 多云 yun null null null null 23.1℃ 4℃ 6℃ [\"西北风\",\"东南风\"] 3-4级转<3级";
//		String string = str.substring(3,5);
//		Integer db = Integer.valueOf(string);
//		System.out.println(db);
		String[] split = str.split(" ");
//		for(String str1 : split) {
//			System.out.println(str1);
//		}
		String[] fileds = split;
		int cityid = Integer.parseInt(fileds[0]);
		int week = Integer.parseInt(fileds[2]);
		double tem1 = temToDouble(fileds[11]);
		double tem2 = temToDouble(fileds[12]);
		double tem = temToDouble(fileds[13]);

		WeaBean weaBean = new WeaBean(cityid, week, tem1, tem2, tem);

		System.out.println(weaBean);
		return weaBean;

//		WeaBean weaBean = new WeaBean(cityid, week, city, tem1, tem2, tem);

//		System.out.println(weaBean);
//		System.out.println(weaBean.getTem1().substring(0,weaBean.getTem1().length()-1));

	}

	public Double temToDouble(String tem) {
		String trimTem = tem.substring(0, tem.length() - 1);
		return Double.parseDouble(trimTem);
	}

	@org.junit.Test
	public void reduceTest() {
		String str1 = "101340404 南投 2 2019-08-09 11:30:00 中雨转小雨 yu null null null null 25℃ 21℃ 23℃ [\"南风\",\"南风\"] 4-5级转3-4级";
		String str2 = "101340404 南投 3 2019-08-09 11:30:00 小雨转阴 yu null null null null 25℃ 20℃ 23℃ [\"南风\",\"南风\"] 3-4级转<3级";
		String str3 = "101301211 大化 3 2019-08-09 11:30:00 多云转阴 yun null null null null 35℃ 25℃ 26℃ [\"北风\",\"北风\"] <3级";
		WeaBean bean1 = mapTest(str1);
		WeaBean bean2 = mapTest(str2);
		WeaBean bean3 = mapTest(str3);
		List<WeaBean> values = Arrays.asList(bean1, bean2, bean3);

		int cityid = 0;
		double tem = 0;

		double maxLowTem = 0, minLowTem = 100, averLowTem = 0;

		double maxHighTem = 0, minHighTem = 100, averHighTem = 0;

		double sumHigh = 0, sumLow = 0;

		int count = 0;

		for (WeaBean value : values) {
			if (count == 0) {
				cityid = value.getCityid();
				tem = value.getTem();
//				throw new RuntimeException(key + ":"+value.toString());
				// 问题可能是由于bean中写入字符串方法导致
//				WeatherBean [cityid=1010101006åäº¬32â22â26â, week=null, city=null, tem1=null, tem2=null, tem=null]
			}
			double highTem = value.getTem1();// 高温

			double lowTem = value.getTem2();// 低温

			sumHigh += highTem;
			if (highTem > maxHighTem)
				maxHighTem = highTem;
			if (highTem < minHighTem)
				minHighTem = highTem;

			sumLow += lowTem;
			if (lowTem > maxLowTem)
				maxLowTem = lowTem;
			if (lowTem < minLowTem)
				minLowTem = lowTem;
			count++;
		}
		averHighTem = sumHigh / count;
		averHighTem = Double.parseDouble(String.format("%.1f", averHighTem));

		averLowTem = sumLow / count;
		averLowTem = Double.parseDouble(String.format("%.1f", averLowTem));
		WeaStatBean statBean = new WeaStatBean(cityid, tem, maxLowTem, minLowTem, averLowTem, maxHighTem, minHighTem,
				averHighTem);
		System.out.println(statBean);
	}

}
